package com.ruoyi.project.order.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.income.domain.BinsMerchantAccount;
import com.ruoyi.project.income.service.IBinsMerchantAccountService;
import com.ruoyi.project.order.service.date.DateUtil;
import com.ruoyi.project.price.domain.BinsAccount;
import com.ruoyi.project.price.domain.BinsAccountTrace;
import com.ruoyi.project.price.domain.BinsPriceRule;
import com.ruoyi.project.price.service.IBinsAccountService;
import com.ruoyi.project.price.service.IBinsAccountTraceService;
import com.ruoyi.project.price.service.IBinsPriceRuleService;
import com.ruoyi.project.user.domain.UserInfo;
import com.ruoyi.project.user.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.order.mapper.BinsOrderMapper;
import com.ruoyi.project.order.domain.BinsOrder;
import com.ruoyi.project.order.service.IBinsOrderService;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-01
 */
@Service
public class BinsOrderServiceImpl implements IBinsOrderService 
{
    @Autowired
    private BinsOrderMapper binsOrderMapper;
    @Autowired
    Snowflake snowflake;

    /**
     * 查询订单信息
     * 
     * @param orderNo 订单信息主键
     * @return 订单信息
     */
    @Override
    public BinsOrder selectBinsOrderByOrderNo(String orderNo)
    {
        return binsOrderMapper.selectBinsOrderByOrderNo(orderNo);
    }

    /**
     * 查询订单信息列表
     * 
     * @param binsOrder 订单信息
     * @return 订单信息
     */
    @Override
    public List<BinsOrder> selectBinsOrderList(BinsOrder binsOrder)
    {
        return binsOrderMapper.selectBinsOrderList(binsOrder);
    }

    /**
     * 新增订单信息
     * 
     * @param binsOrder 订单信息
     * @return 结果
     */
    @Override
    public int insertBinsOrder(BinsOrder binsOrder)
    {
        binsOrder.setOrderNo(snowflake.nextIdStr());
        binsOrder.setOrderStatus(0);
        binsOrder.setStartTime(DateUtils.getTime());
        binsOrder.setCreateTime(DateUtils.getNowDate());
        return binsOrderMapper.insertBinsOrder(binsOrder);
    }

    /**
     * 修改订单信息
     * 
     * @param binsOrder 订单信息
     * @return 结果
     */
    @Override
    public int updateBinsOrder(BinsOrder binsOrder)
    {
        binsOrder.setUpdateTime(DateUtils.getNowDate());
        return binsOrderMapper.updateBinsOrder(binsOrder);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderNos 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteBinsOrderByOrderNos(String[] orderNos)
    {
        return binsOrderMapper.deleteBinsOrderByOrderNos(orderNos);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderNo 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteBinsOrderByOrderNo(String orderNo)
    {
        return binsOrderMapper.deleteBinsOrderByOrderNo(orderNo);
    }

    @Autowired
    private IBinsAccountTraceService accountTraceService;
    @Autowired
    private IBinsAccountService binsAccountService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IBinsMerchantAccountService merchantAccountService;

    @Override
    public BinsOrder payOrder(String orderNo, String paySource) {
        BinsOrder binsOrder = binsOrderMapper.selectBinsOrderByOrderNo(orderNo);
        if (binsOrder!=null){
            BinsAccountTrace binsAccountTrace = accountTraceService.getAccountTrace2Pay(binsOrder.getUserId(), binsOrder.getOrderPrice().negate(), "0");
            // 更新订单
            binsOrder.setOrderStatus(3);
            binsOrder.setPayTime(new Date());
            binsOrder.setPayId(binsAccountTrace.getTraceSn());
            binsOrder.setUpdateTime(new Date());
            binsOrderMapper.updateBinsOrder(binsOrder);

            // 更新商家账户余额
            BinsMerchantAccount binsMerchantAccount_data = merchantAccountService.selectBinsMerchantAccountByMerchantDeptId(binsOrder.getDeptId());
            if (binsMerchantAccount_data==null){
                binsMerchantAccount_data = new BinsMerchantAccount();
                binsMerchantAccount_data.setMerchantDeptId(binsOrder.getDeptId());
                binsMerchantAccount_data.setTotalAmount(BigDecimal.ZERO);
                binsMerchantAccount_data.setAvailable(BigDecimal.ZERO);
                binsMerchantAccount_data.setCreateTime(new Date());
                merchantAccountService.insertBinsMerchantAccount(binsMerchantAccount_data);
            }
            BinsMerchantAccount binsMerchantAccount = new BinsMerchantAccount();
            binsMerchantAccount.setMerchantDeptId(binsMerchantAccount_data.getMerchantDeptId());
            binsMerchantAccount.setTotalAmount(binsMerchantAccount_data.getTotalAmount().add(binsOrder.getOrderPrice()));
            binsMerchantAccount.setAvailable(binsMerchantAccount_data.getAvailable().add(binsOrder.getOrderPrice()));
            merchantAccountService.updateBinsMerchantAccount(binsMerchantAccount);
            return binsOrder;
        }

        return binsOrderMapper.selectBinsOrderByOrderNo(binsOrder.getOrderNo());
    }

    @Autowired
    IBinsPriceRuleService priceRuleService;
    @Override
    public void updateOrderPrice() {
        BinsOrder binsOrder = new BinsOrder();
        binsOrder.setOrderStatusList(Arrays.asList(0,1));
        List<BinsOrder> binsOrders = binsOrderMapper.selectBinsOrderList(binsOrder);
        if (binsOrders!=null && binsOrders.size()>0){
            for (BinsOrder orderInfo : binsOrders) {
                String startTime = orderInfo.getStartTime();
                Long deptId = orderInfo.getDeptId();
                if (deptId==null){
                    continue;
                }
                String vehicleType = orderInfo.getVehicleType();
                if (StringUtils.isNotBlank(startTime)) {
                    BinsPriceRule priceRule = new BinsPriceRule();
                    priceRule.setDeptId(deptId);
//                    priceRule.setVehicleType(vehicleType);
                    priceRule.setPriceRule("分钟");
                    priceRule.setProductType("0");
                    BinsPriceRule binsPriceRule_current = priceRuleService.selectBinsPriceRule(priceRule);
                    String onceMins = binsPriceRule_current.getOneMins();
                    long mins = DateUtil.getDatePoorAsMins(DateUtil.getDate(startTime, DateUtil.YYYY_MM_DD_HH_MM_SS), new Date());
                    long count = mins / Long.parseLong(onceMins);
                    long nextMins = Long.parseLong(onceMins);
                    count = count + (nextMins > 0 ? 1 : 0);
                    BigDecimal orderPrice = binsPriceRule_current.getPrice().multiply(new BigDecimal(count));

                    orderInfo.setOrderPrice(orderPrice);
                    orderInfo.setOrderMins(Integer.parseInt(String.valueOf(mins)));
                    orderInfo.setUpdateTime(DateUtils.getNowDate());
                    binsOrderMapper.updateBinsOrder(orderInfo);
                }
            }
        }
    }

    public static void main(String[] args) {
        String onceMins = "16";
        long mins = DateUtil.getDatePoorAsMins(DateUtil.getDate("2025-04-18 10:58:35", DateUtil.YYYY_MM_DD_HH_MM_SS), new Date());
        long count = mins/Long.parseLong(onceMins);
        long nextMins = Long.parseLong(onceMins);
        count = count + (nextMins>0?1:0);
        System.out.println(count);
        System.out.println(mins%Long.parseLong(onceMins));
    }
}
